﻿// --------------------------------------------------------------------------------------------------------------------
// <copyright file="Program.cs" company="Roman '4ux-nbIx' Novitsky">
//   Copyright (c) Roman '4ux-nbIx' Novitsky 2010
// </copyright>
// <summary>
//   Defines the Program type.
// </summary>
// --------------------------------------------------------------------------------------------------------------------

namespace MtgMC
{
  #region Namespace Imports

  using System;
  using System.IO;
  using System.Reflection;
  using System.Windows.Forms;

  using DataModel;

  #endregion


  /// <summary>
  /// Describes and manipulates the program.
  /// </summary>
  internal static class Program
  {
    #region Constants and Fields

    private static ApplicationData _data;

    #endregion


    #region Methods

    internal static void SaveState()
    {
      string fileName = GetDataFileName();

      try
      {
        ApplicationData.Save(_data, fileName);
      }
      catch (Exception e)
      {
        ShowErrorMessageBox(e, "Error saving application data.", MessageBoxButtons.OK);

        throw;
      }
    }


    private static string GetCurrentDirectory()
    {
      string currentDirectory = GetExecutableFileName();
      currentDirectory = Path.GetDirectoryName(currentDirectory);

      return currentDirectory;
    }


    private static string GetDataFileName()
    {
      string currentDirectory = GetCurrentDirectory();

      return Path.Combine(currentDirectory, "Data.txt");
    }


    /// <summary>
    /// Gets the name of the executable file.
    /// </summary>
    /// <returns>
    /// A path to the application executable file.
    /// </returns>
    private static string GetExecutableFileName()
    {
      return Assembly.GetExecutingAssembly().ManifestModule.FullyQualifiedName;
    }


    private static ApplicationData LoadState()
    {
      string fileName = GetDataFileName();

      if (!File.Exists(fileName))
      {
        var applicationData = new ApplicationData();

        var player = new Player { Name = "Player" };
        applicationData.Player = player;

        return applicationData;
      }

      try
      {
        return ApplicationData.Load(fileName);
      }
      catch (Exception e)
      {
        ShowErrorMessageBox(e, "Error loading application data.", MessageBoxButtons.OK);

        Application.Exit();

        throw;
      }
    }


    /// <summary>
    /// The main entry point for the application.
    /// </summary>
    [MTAThread]
    private static void Main()
    {
      _data = LoadState();

      Application.Run(new MainForm(_data));

      SaveState();
    }


    private static DialogResult ShowErrorMessageBox(Exception e, string errorText, MessageBoxButtons messageBoxButtons)
    {
      errorText = errorText + "\n" + e.Message;

      return MessageBox.Show(
        errorText, "Error", messageBoxButtons, MessageBoxIcon.Hand, MessageBoxDefaultButton.Button1);
    }

    #endregion
  }
}